package cn.wolfcode.shop.service.impl;

import cn.wolfcode.shop.domain.Catalog;
import cn.wolfcode.shop.mapper.CatalogMapper;
import cn.wolfcode.shop.mapper.PropertyMapper;
import cn.wolfcode.shop.service.ICatalogService;
import cn.wolfcode.shop.util.RedisConstants;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class CatalogServiceimpl implements ICatalogService {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private CatalogMapper catalogMapper;
    @Autowired
    private PropertyMapper propertyMapper;
    @Override
    public List<Catalog> listAll() {
        //获取redis缓存(所有商品类别)
        List<Catalog> catalogs = (List<Catalog>)redisTemplate.opsForValue().get(RedisConstants.CATALOG_ALL);
        if(catalogs==null){//不存在该缓存
            catalogs = catalogMapper.selectAll();
            //将商品类别信息设置到redis里
            redisTemplate.opsForValue().set(RedisConstants.CATALOG_ALL,catalogs);
        }
        return catalogs;
    }

    @Override
    public List<Catalog> listByPid(Long pid) {
        List<Catalog> catalogList=catalogMapper.selectByPid(pid);
        catalogList.forEach(catalog -> {
            Integer count=(Integer) redisTemplate.opsForValue().get(RedisConstants.CATALOG_PROPERTYS_COUNT
                    .replaceAll("CATALOG_ID",catalog.getId().toString()));
            if(count==null){
                count= propertyMapper.selectForCountByCatalogId(catalog.getId());
                redisTemplate.opsForValue().set(RedisConstants.CATALOG_PROPERTYS_COUNT
                        .replace("CATALOG_ID",catalog.getId().toString()),count);
            }
            catalog.setPropertyCount(count);
        });
        return catalogList;
    }

    @Override
    public void saveOrUpdate(Catalog catalog) {
        if(catalog.getId()==null){//新增
            catalogMapper.insert(catalog);
        }else{ //更新
            Catalog cl = catalogMapper.selectByPrimaryKey(catalog.getId());
            cl.setName(catalog.getName());
            cl.setCode(catalog.getCode());
            cl.setSort(catalog.getSort());
            cl.setPId(catalog.getPId());
            cl.setIsParent(catalog.getIsParent());
            catalogMapper.updateByPrimaryKey(cl);
        }
        refleshRedis();
    }

    @Override
    public void updateSort(List<Long> ids) {
        if(ids!=null&&ids.size()>0){
            for (int i = 0; i < ids.size(); i++) {
                Catalog catalog = catalogMapper.selectByPrimaryKey(ids.get(i));
                catalog.setSort(i+1);
                catalogMapper.updateByPrimaryKey(catalog);
            }
        }
        refleshRedis();
    }

    @Override
    public void deleteById(Long id) {
        catalogMapper.deleteByPrimaryKey(id);
        refleshRedis();
    }

    /**
     * 更新缓存
     */
    public List<Catalog> refleshRedis(){
        List<Catalog> catalogs = catalogMapper.selectAll();
        redisTemplate.opsForValue().set(RedisConstants.CATALOG_ALL,catalogs);
        return catalogs;
    }
}
